<template>
  <view>
    <!-- search组件 -->
    <my-search @click="gotoSearch"></my-search>
    <view class="view-scroll-contanier">
      <!-- 左侧滑动区域 -->
      <scroll-view scroll-y="true" :style="{height:wh +'px'}" class="view-scroll-left">
        <block v-for="(item,i) in goodList" :key="i">
          <view :class="['view-scroll-left-item', i === active?'active':'']" @click="getactive(i)">{{item.cat_name}}</view>
        </block>
        
        
      </scroll-view>
      <!-- 右侧滑动区域 -->
      <scroll-view scroll-y="true"  :style="{height:wh +'px'}" :scroll-top="scrollTop">
       <!-- 二级分类 -->
        <view class="cart-lv2">
          <view class="cart-lv2-item" v-for="(item2,i2) in cartlevel" :key="i2">
            <view class="cart-lv2-text">/ {{item2.cat_name}} /</view>
            <!-- 二级分类下的三级分类列表 -->
            <view class="cart-lv3">
              <!-- 三级分类项 -->
              <view class="cart-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodList(item3)">
                <!-- 三级分类的图片 -->
                <image :src="item3.cat_icon" ></image>
                <!-- 三级分类的标题 -->
                <text >{{item3.cat_name}}</text>
              </view>
            </view>
            
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {    
        wh:0 ,// 可使用窗口高度
        goodList:[], //商品数据
        active:0,
        cartlevel:[], //二级分类的数据
        scrollTop:0
      };
    },
    onLoad(){
      // 获取可使用窗口高度
      const info = uni.getSystemInfoSync()
      // console.log(info)
      this.wh=info.windowHeight
      // 为二级分类赋值
      this.getgoodList()
    },
    methods:{
      // 获取商品数据
    async  getgoodList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status!==200) return uni.$showMsg()
        this.goodList=res.message
        this.cartlevel=res.message[0].children
      },
      // 点击左侧滑动区域
      getactive(i){
        // 动态绑定active类名
        this.active=i
        // 动态的为二级分类赋值
        this.cartlevel = this.goodList[i].children
        this.scrollTop=this.scrollTop==0 ? 1 :0
      },
      // 跳转到三级列表页面
      gotoGoodList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+item.cat_id
        })
      },
      gotoSearch(){
        // console.log('ok')
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  // 滑动区域的样式
.view-scroll-contanier {
  display: flex;
  .view-scroll-left {
    width: 120px;
    .view-scroll-left-item {
      background-color: #f7f7f7;
      line-height: 60px;
      font-size: 12px;
      text-align: center;
      &.active {
        position: relative;
        background-color: #FFFFFF;
        &::before {
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
}
// 二级分类文字样式
.cart-lv2-text {
  text-align: center;
  font-size: 12px;
  font-weight: bold;
  padding: 15px 0;
}
// 三级分类样式
.cart-lv3 {
  display: flex;
  flex-wrap: wrap;
  .cart-lv3-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 33.33%;
    margin-bottom: 10px;
    image {
      width: 60px;
      height: 60px;
    }
    text {
      font-size: 12px;
    }
  }
}
</style>
